package com.iaiai.cobra.redis;

import com.iaiai.cobra.redis.keys.RedisAdminKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * Created with RedisAdminCache.
 * Package: com.iaiai.cobra.redis
 * Author: iaiai
 * Create Time: 2024/6/23 13:09
 * QQ: 176291935
 * Email: 176291935@qq.com
 * Description:
 */
@Component
public class RedisAdminCache {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public static final long loginTimeout = 1 * 60 * 60 * 24 * 30;  //秒，过时1月

    //存储登录信息
    public void setLogin(String token,String id, String value) throws Exception {
        redisTemplate.opsForValue().set(RedisAdminKeys.tokenKey + ":" + token, value, loginTimeout, TimeUnit.SECONDS);//token:onelinevo
        redisTemplate.opsForValue().set(String.format("%s:%s:%s",RedisAdminKeys.userKey,id,token), token, loginTimeout, TimeUnit.SECONDS);//memberId:token  token
    }

    public String getLogin(String token){
        return redisTemplate.opsForValue().get(RedisAdminKeys.tokenKey + ":" + token);
    }

    public void removeLogin(String token,String id){
        redisTemplate.delete(RedisAdminKeys.tokenKey + ":" + token);
        redisTemplate.delete(String.format("%s:%s:%s",RedisAdminKeys.userKey,id,token));
    }

}
